package org.jeecg.modules.api.wlhy.customer;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hypricehistory.entity.HyPriceHistory;
import org.jeecg.modules.wlhy.hypricehistory.service.IHyPriceHistoryService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.entity.HyOrderDTO;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @Description: 货主货源
* @Author: jeecg-boot
* @Date:   2020-11-30
* @Version: V1.0
*/
@Api(tags="app_customer")
@RestController
@RequestMapping("/api/wlhy/customer/order")
@Slf4j
public class ApiCustomerOrderController extends JeecgController<HyCustomer, IHyCustomerService> {
   @Autowired
   private IHyCustomerService hyCustomerService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    IHyPriceHistoryService hyPriceHistoryService;

    @Autowired
    IHyCustomerAccountBookService hyCustomerAccountBookService;


    @ApiOperation(value="hy_order-货主指派货源分页列表查询", notes="hy_order-货主指派货源分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HyOrder hyOrder,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
        QueryWrapper<HyOrder> queryWrapper = QueryGenerator.initQueryWrapper(hyOrder, req.getParameterMap());
        queryWrapper.eq("customer_id",hyCustomer.getId());
        queryWrapper.eq("order_type","1");
        if(StringUtils.isNotEmpty(hyOrder.getKeyword())){
            queryWrapper.and(
                    qw -> qw.like("hy_order.send_address_name", hyOrder.getKeyword())
                            .or().like("hy_order.receive_address_name", hyOrder.getKeyword())
            );
        }


        Page<HyOrder> page = new Page<HyOrder>(pageNo, pageSize);
        IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value="hy_order-货主公开货源分页列表查询", notes="hy_order-货主公开货源分页列表查询")
    @GetMapping(value = "/publist")
    public Result<?> publist(HyOrder hyOrder,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
        QueryWrapper<HyOrder> queryWrapper = QueryGenerator.initQueryWrapper(hyOrder, req.getParameterMap());
        queryWrapper.eq("customer_id",hyCustomer.getId());
        queryWrapper.eq("order_type","2");

        if(StringUtils.isNotEmpty(hyOrder.getKeyword())){
            queryWrapper.and(
                    qw -> qw.like("hy_order.send_address_name", hyOrder.getKeyword())
                            .or().like("hy_order.receive_address_name", hyOrder.getKeyword())
            );
        }

        Page<HyOrder> page = new Page<HyOrder>(pageNo, pageSize);
        IPage<HyOrder> pageList = hyOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }





   /* public List<类名> getList(类名 condition,int start,int size){
        //注释的这种写法，筛选是只要condition中栏位有值，sql的where条件中都会自带<等于>判断语句
        //LambdaQueryWrapper<类名> queryWrapper = new QueryWrapper<类名>(condition).lambda()
        LambdaQueryWrapper<类名> queryWrapper = new QueryWrapper<类名>().lambda();
        //<类名>()这种不带的，筛选条件需全部自己写
		...
        return this.page(new Page<>(start,size),queryWrapper).getRecords();
    }*/



   /* *//**
     * 获取用户信息（QueryWrapper查询分页）
     *
     * @param pagination 查询条件
     * @return
     *//*
    public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();


        Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        return this.page(page, queryWrapper);
    }*/



    /* *//**
     * 通过orderNum分页查询
     *
     * @param
     * @return
     */


    @ApiOperation(value="调价历史表-分页列表查询", notes="调价历史表-分页列表查询")
    @GetMapping(value = "/queryPriceHistoryByOrderNumList")
    public Result<?> queryPageList(HyPriceHistory hyPriceHistory,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   @RequestParam(name="orderId") String orderId) {
        if(StringUtils.isEmpty(orderId)){
            return Result.error("货源id不能为空");
        }
        QueryWrapper<HyPriceHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        Page<HyPriceHistory> page = new Page<HyPriceHistory>(pageNo, pageSize);
        IPage<HyPriceHistory> pageList = hyPriceHistoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     *   添加
     *
     * @param hyOrder
     * @return
     */
    @AutoLog(value = "hy_order-货主添加")
    @ApiOperation(value="hy_order-货主添加", notes="hy_order-货主添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HyOrderDTO hyOrder, HttpServletRequest req) {
        Result r = hyOrderService.addOrder(hyOrder);
        return r;
    }

    @AutoLog(value = "hy_order-货主发布货源")
    @ApiOperation(value="hy_order-货主发布货源", notes="hy_order-货主发布货源")
    @PutMapping(value = "/publish")
    public Result<?> publish(@RequestParam(name="id",required=true) String id,HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
        HyOrder hyOrder = hyOrderService.getById(id);
        if(hyOrder == null){
            return Result.error("货源信息不存在");
        }
        String customerId = hyCustomer.getId();
        if(!StringUtils.equals(customerId,hyOrder.getCustomerId())){
            return Result.error("货源信息不存在");
        }
        if(!"0".equals(hyOrder.getOrderStatus())){
            return Result.error("货源信息已发布,不能重复发布");
        }
        hyOrder.setOrderStatus("2");//发布状态
        hyOrder.setDtPublish(new Date());//发布日期

        if( hyOrderService.updateById(hyOrder)) {
            return Result.OK("发布成功");
        }else {
            return Result.error("发布失败");
        }
    }

    @AutoLog(value = "hy_order-货主完结货源")
    @ApiOperation(value="hy_order-货主完结货源", notes="hy_order-货主完结货源")
    @PutMapping(value = "/finish")
    public Result<?> finish(@RequestParam(name="id",required=true) String id,HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
        HyOrder hyOrder = hyOrderService.getById(id);
        if(hyOrder == null){
            return Result.error("货源信息不存在");
        }
        String customerId = hyCustomer.getId();
        if(!StringUtils.equals(customerId,hyOrder.getCustomerId())){
            return Result.error("货源信息不存在");
        }
        if(!"2".equals(hyOrder.getOrderStatus())){
            return Result.error("不能进行次操作");
        }
        hyOrder.setOrderStatus("8");//发布状态
        hyOrder.setDtFinish(new Date());//完结日期

        if( hyOrderService.updateById(hyOrder)) {
            return Result.OK("发布成功");
        }else {
            return Result.error("发布失败");
        }
    }

    /**
     *  编辑
     *
     * @param hyOrder
     * @return
     */
    @AutoLog(value = "hy_order-编辑")
    @ApiOperation(value="hy_order-编辑", notes="hy_order-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyOrderDTO hyOrder) {
        Result r = hyOrderService.editOrder(hyOrder);
        return r;
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "hy_order-通过id删除")
    @ApiOperation(value="hy_order-通过id删除", notes="hy_order-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        HyOrder torder = hyOrderService.getById(id);
        if(torder == null){
            return Result.error("货源信息不存在");
        }
        if(!"0".equals(torder.getOrderStatus())){
            return Result.error("货源信息已发布,不能删除");
        }
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="hy_order-通过id查询", notes="hy_order-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        HyOrder hyOrder = hyOrderService.getById(id);
        if(hyOrder==null) {
            return Result.error("未找到货源信息");
        }
        return Result.OK(hyOrder);
    }

    /* *//**
     * 通过orderNum查询
     *
     * @param
     * @return
     */
    @ApiOperation(value="调价历史表-通过orderNum查询", notes="调价历史表-通过orderNum查询")
    @GetMapping(value = "/queryPriceHistoryByOrderNum")
    public Result<?> queryPriceHistoryByOrderNum(@RequestParam(name="orderNum",required=true) String orderNum) {
        List<HyPriceHistory> hyPriceHistorys = hyPriceHistoryService.getByOrderNum(orderNum);
        if(hyPriceHistorys==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hyPriceHistorys);
    }



}
